package mango.orm.entity;

import java.util.ArrayList;
import java.util.List;

import mango.orm.SQLParameter;



public final class EntityPrmMaker {
    public static List<SQLParameter> getQueryParameters(Entity entity) {
        List<SQLParameter> primarySQLParameters = new ArrayList<SQLParameter>();
        List<SQLParameter> contidionSQLParameters = new ArrayList<SQLParameter>();
        int primaryIndex = 0;
        int conditionIndex = 0;
        for (String column : entity.getFieldNames()) {
            Object val = entity.get(column);
            if (entity.isPrimary(column)) {
                if (val != null) {
                    primaryIndex++;
                    primarySQLParameters.add(new SQLParameter(primaryIndex, val, entity.getField(column).getJdbcType()));
                }
            }
            if (val != null) {
                conditionIndex++;
                contidionSQLParameters.add(new SQLParameter(conditionIndex, val,entity.getField(column).getJdbcType()));
            }
        }
        if (primaryIndex != 0 && entity.getPrimarys().size() == primaryIndex) {
            return primarySQLParameters;
        }
        else if (conditionIndex != 0) {
            return contidionSQLParameters;
        }
        else {
            throw new EntityException("no query condition!");
        }
    }

    public static List<SQLParameter> getInsertParameters(Entity entity) {
        List<SQLParameter> paramters = new ArrayList<SQLParameter>();
        int index = 0;
        for (String column : entity.getFieldNames()) {
            paramters
                    .add(new SQLParameter(++index, entity.get(column), entity.getField(column).getJdbcType()));
        }
        return paramters;
    }

    public static List<SQLParameter> getUpdateParameters(Entity entity) {
        List<SQLParameter> paramters = new ArrayList<SQLParameter>();
        int setIndex = 0;
        int primaryIndex = entity.getFieldNames().size() - entity.getPrimarys().size();
        for (String column : entity.getFieldNames()) {
            Object val = entity.get(column);
            if (entity.isPrimary(column)) {
                if (val == null) {
                    throw new EntityException("primary was null, can't be update!");
                }
                paramters.add(new SQLParameter(++primaryIndex, val, entity.getField(column).getJdbcType()));
            }
            else {
                paramters.add(new SQLParameter(++setIndex, val, entity.getField(column).getJdbcType()));
            }
        }
        return paramters;
    }

    public static List<SQLParameter> getDeleteParameters(Entity entity) {
        List<SQLParameter> paramters = new ArrayList<SQLParameter>();
        int index = 0;
        for (String column : entity.getPrimarys()) {
            Object val = entity.get(column);
            if (val == null) {
                throw new EntityException("primary was null, can't be delete!");
            }
            paramters.add(new SQLParameter(++index, val, entity.getField(column).getJdbcType()));
        }
        return paramters;
    }
}
